/**
 * @file rgb1.c
 * @author your name (you@domain.com)
 * @brief RGB24格式的每个像素的三个分量是连续存储的。一帧宽高分别为w、h的RGB24图像一共占用w*h*3 Byte的存储空间。RGB24格式规定首先存储第一个像素的R、G、B，然后存储第二个像素的R、G、B…以此类推。
 *        类似于YUV420P的存储方式称为Planar方式，而类似于RGB24的存储方式称为Packed方式。将分离后的文件，以Y 的方式在YUV播放器中可以正常显示
 * @version 0.1
 * @date 2021-04-27
 * 
 * @copyright Copyright (c) 2021
 * 
 */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int simplest_rgb24_split(char *url, int w, int h,int num);

int main(void)
{
    simplest_rgb24_split("../resource/cie1931_500x500.rgb", 500, 500, 1);
    return 0;
}

int simplest_rgb24_split(char *url, int w, int h,int num)
{
    FILE * fp = fopen(url, "rb+");
    FILE * fp1 = fopen("output_r.y", "wb+");
    FILE * fp2 = fopen("output_g.y", "wb+");
    FILE * fp3 = fopen("output_b.y", "wb+");

    uint8_t * pic = (uint8_t*)malloc(w * h *3);
    for (int i = 0; i < num; i++)
    {
        fread(pic, 1, w * h * 3, fp);
        for (int j = 0; j < w * h * 3; j+=3)
        {
            fwrite(pic + j + 0, 1, 1, fp1);
            fwrite(pic + j + 1, 1, 1, fp2);
            fwrite(pic + j + 2, 1, 1, fp3);
        }
        
    }

    free(pic);

    fclose(fp);
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);

    return 0;   
}